package com.woniuxy.stage3demo.mapper.otherPzc;

import com.woniuxy.stage3demo.bean.otherPzc.AdsBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AdsMapper {
    /**
     * 新增广告
     * @param adsBean 新增的广告对象
     * @return 数据库操作的行数
     */
    @Options(useGeneratedKeys = true,keyProperty = "a.id")
    @Insert("Insert into ads"+
            "(ads_no,ads_name,ads_location,begin_time,end_time,img_src,src,note,click_num,to_order,status,del_flag)"+
            "values(#{a.adsNo},#{a.adsName},#{a.adsLocation},#{a.beginTime},#{a.endTime},#{a.imgSrc},#{a.src},#{a.note},0,0,1,0)")
    int saveAds(@Param("a")AdsBean adsBean);
    /**
     * 删除广告
     * @param id 删除的广告对象的id
     * @return 数据库操作的行数
     */
    @Update("update ads set del_flag=1 where id=#{id}")
    int deleteAds(@Param("id") int id);

    /**
     * 修改广告
     * @param adsBean 修改的广告对象
     * @return 操作行数
     */
    @Update("<script>" +
            "update ads" +
            "<set>" +
            "<if test='a.adsNo != null'>" +
            " ads_no = #{a.adsNo}," +
            "</if>" +
            "<if test='a.adsName != null'>" +
            " ads_name = #{a.adsName}," +
            "</if>" +
            "<if test='a.adsLocation != null'>" +
            " ads_location = #{a.adsLocation}," +
            "</if>" +
            "<if test='a.beginTime != null'>" +
            " begin_time = #{a.beginTime}," +
            "</if>" +
            "<if test='a.endTime != null'>" +
            " end_time = #{a.endTime}," +
            "</if>" +
            "<if test='a.imgSrc != null'>" +
            " img_src = #{a.imgSrc}," +
            "</if>" +
            "<if test='a.src != null'>" +
            " src = #{a.src}," +
            "</if>" +
            "<if test='a.note != null'>" +
            " note = #{a.note}," +
            "</if>" +
            "<if test='a.clickNum != null'>" +
            " clickNum = #{a.clickNum}," +
            "</if>" +
            "<if test='a.toOrder != null'>" +
            " toOrder = #{a.toOrder}," +
            "</if>" +
            "<if test='a.status != null'>" +
            " status = #{a.status}," +
            "</if>" +
            "</set>" +
            "where id = #{a.id}" +
            "</script>")
    int updateAds(@Param("a") AdsBean adsBean);

    /**
     * 分页查询
     *
     * @param request
     * @param adsBean
     * @return
     */
    List<AdsBean> findAllByParams(@Param("p")PageRequest request, @Param("a")AdsBean adsBean);
}
